﻿package com.lsmo.android.util;

import java.io.File;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Environment;
import android.os.StatFs;
import android.telephony.TelephonyManager;
import android.util.Log;

/**
 * 获取硬件信息类
 * @项目名称：carfans     
 * @类名称：GetHardware
 * @修改时间：2012-3-26 
 * @修改备注：  
 */
public class GetHardware {
	
	private Context mContext;
	public GetHardware(Context context) {
		this.mContext = context;
	}
	
	/**获取硬件信息**/
	/**
	 * 获取IMEI码
	 * @return
	 */
	public String getIMEI() {
		TelephonyManager tm = (TelephonyManager) mContext
				.getSystemService(Context.TELEPHONY_SERVICE);
		String imei = tm.getDeviceId();
		return imei;
	}

	/**
	 * 获取telephone号码
	 * @return
	 */
	public String getTelephone() {
		TelephonyManager tm = (TelephonyManager) mContext
				.getSystemService(Context.TELEPHONY_SERVICE);
		String telephone = tm.getLine1Number();
		return telephone;
	}

	/**
	 * 获取SIM卡号码
	 * @return
	 */
	public String getSIM() {
		TelephonyManager tm = (TelephonyManager) mContext
				.getSystemService(Context.TELEPHONY_SERVICE);
		String sim = tm.getSimSerialNumber();
		return sim;
	}

	/**
	 * 获取IMSI码
	 * @return
	 */
	public String getIMSI() {
		TelephonyManager tm = (TelephonyManager) mContext
				.getSystemService(Context.TELEPHONY_SERVICE);
		String imsi = tm.getSubscriberId();
		return imsi;
	}

	/**
	 * 获取ip地址
	 * @return
	 */
	public String getLocalIpAddress() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf
						.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						return inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (SocketException ex) {
			Log.e("WifiPreference IpAddress", ex.toString());
		}
		return null;
	}

	/**
	 * 获取mac地址
	 * @return
	 */
	public String getLocalMacAddress() {
		WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = wifi.getConnectionInfo();
		return info.getMacAddress();
	}

	/**
	 * 获取sdcard路径
	 * @return
	 */
	public String getSdCardPath() {
		String noSdCard = "没有sdcard!";
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			File sdDir = Environment.getExternalStorageDirectory();
			return sdDir.toString();
		} else {
			return noSdCard;
		}
	}

	/**
	 * 获取sdcard存储大小
	 */
	public long getSdCardSize() {
		File sdDir = Environment.getExternalStorageDirectory();
		StatFs statfs = new StatFs(sdDir.getPath());
		//获取block的SIZE
		long blocSize = statfs.getBlockSize();
		//获取BLOCK数量
		long totalBlocks = statfs.getBlockCount();
		//空闲的Block的数量
		long availaBlock = statfs.getAvailableBlocks();
		//总空间大小(MB)
		long total = blocSize * totalBlocks /1024 /1024 /1024;
		//空闲空间大小(MB)
		long availale = blocSize * availaBlock /1024 /1024;
		
		return total;
	}
}
